package com.framework.utils.ali;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectResult;
import com.framework.utils.ali.entity.AliOssDownResult;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;

/**
 *
 * 阿里云上传工具类
 * @author wq
 */
public class AliOssUtils {

    private static final String endpoint = "oss-cn-beijing.aliyuncs.com";

    private static final String accessKeyId = "LTAInTZVEFiyk6sS";

    private static final String accessKeySecret = "7fjctqfznnmRn6evvP2fUeBBzS9crE";

    private static final String bucketName = "mpos-img";

    public static AliOssDownResult downLoad(String objectName){
        // 创建OSSClient实例。
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

        // 下载OSS文件到获取流
        OSSObject object = ossClient.getObject(bucketName, objectName);
        InputStream inputStream = object.getObjectContent();
        // 关闭OSSClient。
       // ossClient.shutdown();
        AliOssDownResult aliOssDownResult=new AliOssDownResult();
        aliOssDownResult.setInputStream(inputStream);
        aliOssDownResult.setOssClient(ossClient);
        return aliOssDownResult;
    }

    public static void downloadToFile(String objectName,String downloadUrl){
        // 创建OSSClient实例。
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

        // 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖，不存在则新建。
        ossClient.getObject(new GetObjectRequest(bucketName, objectName),new File(downloadUrl));

        // 关闭OSSClient。
        ossClient.shutdown();

    }


    public static void deleteFile(String objectName){
        OSSClient ossClient=new OSSClient(endpoint,accessKeyId,accessKeySecret);
        ossClient.deleteObject(bucketName,objectName);
        ossClient.shutdown();

    }

    /**
     * 上传二进制流
     * @param byteArrayInputStream
     * @param objectName
     * @return
     */
    public static String uploadBytes(ByteArrayInputStream byteArrayInputStream, String objectName){
        // 创建OSSClient实例。
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId,accessKeySecret);

        // 上传Byte数组。
        ossClient.putObject(bucketName, objectName, byteArrayInputStream);

        // 关闭OSSClient。
        ossClient.shutdown();
        return objectName;
    }

    /**
     * 根据路径上传
     * @param filePath
     * @param objectName
     */
    public static void uploadByPath(String filePath,String objectName){
        // 创建OSSClient实例。
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId,accessKeySecret);
        PutObjectResult objectResult = ossClient.putObject(bucketName, objectName, new File(filePath));

        ossClient.shutdown();
    }

    /**
     * 上传文件
     * @param objectName
     * @param file
     */
    public static String uploadByFile(String objectName,File file){

        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

        ossClient.putObject(bucketName, objectName, file);

        ossClient.shutdown();

        return objectName;
    }


}
